package com.ab.scaleprintdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.aibao.serialportlibrary.Device;
import com.aibao.serialportlibrary.SerialPortFinder;
import com.aibao.serialportlibrary.SerialPortManager;
import com.aibao.serialportlibrary.listener.OnOpenSerialPortListener;
import com.aibao.serialportlibrary.listener.OnSerialPortDataListener;

import java.io.File;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private SerialPortManager serialPortManager;
    private TextView tvConnect, tvResult;
    private boolean isOpen = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvConnect = findViewById(R.id.tv_connect);
        tvConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isOpen) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            disconnect();
                        }
                    }).start();
                } else {
                    Toast.makeText(MainActivity.this, "start connect", Toast.LENGTH_SHORT).show();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            connect();
                        }
                    }).start();
                }
            }
        });
        tvResult = findViewById(R.id.tv_receive);
    }

    private void connect() {
        Device connectDevice = null;
        List<Device> devices = new SerialPortFinder().getDevices();
        for (int i = 0; i < devices.size(); i++) {
            Log.d("scaleConnect", "i = " + i + " deviceName=" + devices.get(i).getName());
            if ("ttyS9".equals(devices.get(i).getName())) {
                connectDevice = devices.get(i);
                break;
            }
        }
        if (connectDevice == null) {
            Log.d("scaleConnect", "connectDevice is null!");
            return;
        }
        serialPortManager = new SerialPortManager();
        //Set callback
        serialPortManager.setOnOpenSerialPortListener(new OnOpenSerialPortListener() {
            @Override
            public void onSuccess(File device) {
                Log.d("scaleConnect", "Serial port successfully opened");
            }

            @Override
            public void onFail(File device, OnOpenSerialPortListener.Status status) {
                Log.d("scaleConnect", "Failed to open the serial port");
            }
        }).setOnSerialPortDataListener(new OnSerialPortDataListener() {
            @Override
            public void onDataReceived(String value) {
                Log.d("scaleConnect", "onDataReceived value=" + value);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvResult.setText("result : " + value);
                    }
                });
            }

            @Override
            public void onDataSent(byte[] bytes) {

            }
        });
        //Open the serial port
        isOpen = serialPortManager.openSerialPort(connectDevice, 9600);
        Log.d("scaleConnect", "openSerialPort result = " + isOpen);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (isOpen) {
                    Toast.makeText(MainActivity.this, "connect successfully", Toast.LENGTH_SHORT).show();
                    tvConnect.setText("disconnect");
                }
            }
        });
    }

    private void disconnect() {
        if (serialPortManager != null) {
            serialPortManager.closeSerialPort();
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this, "disconnect", Toast.LENGTH_SHORT).show();
                isOpen = false;
                tvConnect.setText("connect");
                tvResult.setText("result : ");
            }
        });
    }

}